package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.GoodsMapper;
import com.qf.myexception.ServiceException;
import com.qf.pojo.Goods;
import com.qf.service.GoodsService;
import com.qf.utils.Constant;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public ResultMsg findAll() {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_delete", 0);
        List<Goods> list = this.list(wrapper);
        if (list != null) {
            return ResultMsg.SUCCESS(list);
        }
        throw new ServiceException(201, "查询异常");
    }

    @Override
    public ResultMsg findOne(Integer goodsId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goodsId);
        Goods one = this.getOne(wrapper);
        if (one != null) {
            return ResultMsg.SUCCESS(one);
        }
        throw new ServiceException(201, "查询异常");
    }

    @Override
    public ResultMsg findAllByKeywords(String keyword) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.like("goods_name", keyword);
        wrapper.eq("goods_delete", Constant.NO_DELETE);
        List<Goods> list = this.list(wrapper);
        return ResultMsg.SUCCESS(list);
    }
}
